﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Reflection;

namespace Tum.CollabXT.Wizard
{
    /// <summary>
    /// Interaction logic for AboutBox.xaml
    /// </summary>
    public partial class AboutBox : Window
    {
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="pluginManager">Plugin manager to be used.</param>
        public AboutBox(PluginManager pluginManager)
        {
            InitializeComponent();

            Assembly coreAssembly = Assembly.GetAssembly(typeof(IConversionWorkflow));
            Assembly wizardAssembly = Assembly.GetExecutingAssembly();
            runVersion.Text = " Ver. " + coreAssembly.GetName().Version + " (core), " + wizardAssembly.GetName().Version + " (wizard)";

            Paragraph pluginParagraph = new Paragraph();
            pluginParagraph.Inlines.Add(new Bold(new Run("Plugins:")));
            pluginParagraph.Inlines.Add(new LineBreak());

            List<ProviderInfo> providers = new List<ProviderInfo>();
            providers.AddRange(pluginManager.GetProcessProviders());
            providers.AddRange(pluginManager.GetToolProviders());

            foreach(ProviderInfo pi in providers)
            {
                Span span = new Span();
                span.Inlines.Add(new Run((pi is ProcessProviderInfo ? "Process" : "Tool") + " provider: "));
                span.Inlines.Add(new Bold(new Run(pi.Name)));
                span.Inlines.Add(new LineBreak());

                //Write assembly version
                try
                {
                    Assembly asm = Assembly.LoadFile(pi.AssemblyPath);
                    span.Inlines.Add(new Run(" Ver. " + asm.GetName().Version.ToString()));
                    span.Inlines.Add(new LineBreak());
                }
                catch(Exception) { }

                //Add author
                if (pi.Author.Length > 0)
                {
                    span.Inlines.Add(new Run(pi.Author));
                    span.Inlines.Add(new LineBreak());
                }
                span.Inlines.Add(new LineBreak());

                pluginParagraph.Inlines.Add(span);
            }

            textBoxModules.Document.Blocks.Add(pluginParagraph);
        }

        /// <summary>
        /// Event handler for mouse down.
        /// </summary>
        private void labelLink_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if(sender == null || !(sender is TextBlock))
                return;
            try
            {
                Process.Start((sender as TextBlock).Text);
            }
            catch(Exception) { }
        }
    }
}